*Program name:      analyze_horserace_2bins.do
*Written by:        Ezra Karger
*Date written:      April 25, 2020
*Last modified by:  Ezra Karger
*Date modified:     April 28, 2020

clear all



*Set sub-directory paths--same for all users:

global unacast  "${machine}/data/clean/unacast"
global stay_at_home "${machine}/data/clean/stay_at_home"
global womply "${machine}/data/clean/womply"
global nyt_cases "${machine}/data/clean/nyt_cases"
global census "${machine}/data/clean/census"
global horserace "${machine}/data/clean/horserace"
global trump "${machine}/data/clean/trump"
global acs "${machine}/data/clean/acs_2018"
global usda "${machine}/data/clean/usda_ers"
global tables "${machine}/tables/horserace"

global figures "${machine}/figures/event_time"
global logs  "${machine}/programs"


capture log close
log using "${logs}/analyze_horserace_2bins.log", text replace


*Read in county-level dataset to analyze:

use "${horserace}/horserace_data.dta", clear


*Regress each d_* variable on covariates. Start with quantiles of a few interesting variables

foreach var of varlist median_household_income_2018 gop_win_pct ruralurban_continuum_code_2013 effective_date{
	xtile q_`var' = `var', n(2)
}

*Focus on five outcomes:
*		1. change in each outcome from March 1, 2020 - April 11, 2020
*		2. change in outcome from t-10 to t-1 (pre-order)
*		3. change in outcome from t-1 to t+1 (immediate response)
*		4. change in outcome from t+1 to t+10 (post-order response)
*		5. change in outcome from t-10 to t+10 (around-order response)

local outcomes "distance visit pe_r_food pe_r_other pe_r_all log_pe_food log_pe_other log_pe_all"

foreach o of local outcomes {

	di "Results for outcome: `o'"

	gen d_`o'_all =    `o'_val_apr11 -    `o'_val_march1
	gen d_`o'_pre =    `o'_val_event_m0 - `o'_val_event_m10
	gen d_`o'_event =  `o'_val_event_p2-  `o'_val_event_m0
	gen d_`o'_post =   `o'_val_event_p10- `o'_val_event_p2
	gen d_`o'_around = `o'_val_event_p10- `o'_val_event_m10

	gen temp = abs(d_`o'_around - ( d_`o'_pre + d_`o'_event + d_`o'_post )) if ///
		!missing(`o'_val_apr11) & !missing(`o'_val_march1) & ///
		!missing(`o'_val_event_m0) & !missing(`o'_val_event_p2) & ///
		!missing(`o'_val_event_m10) & !missing(`o'_val_event_p10)
	
	sum temp, d
	
	assert temp<0.05 if ///
		!missing(`o'_val_apr11) & !missing(`o'_val_march1) & ///
		!missing(`o'_val_event_m0) & !missing(`o'_val_event_p2) & ///
		!missing(`o'_val_event_m10) & !missing(`o'_val_event_p10)
	drop temp

	foreach var of varlist d_* {

		preserve
		
		di substr("`var'",-3,.)
		
		if (substr("`var'",-3,.)=="all") {
			keep if !missing(`o'_val_apr11) & !missing(`o'_val_march1)
		}
		else if (substr("`var'",-3,.)=="pre") {
			keep if !missing(`o'_val_event_m1) & !missing(`o'_val_event_m10)
		}
		else if (substr("`var'",-3,.)=="ent") {
			keep if !missing(`o'_val_event_p2) & !missing(`o'_val_event_m0)
		}
		else if (substr("`var'",-3,.)=="ost") {
			keep if !missing(`o'_val_event_p10) & !missing(`o'_val_event_p2)
		}
		else if (substr("`var'",-3,.)=="und") {
			keep if !missing(`o'_val_event_p10) & !missing(`o'_val_event_m10)
		}
		else exit
		
		sum `var' [aweight=pop_estimate_2018], d
		estadd scalar mean_d =r(mean)
		
		areg `var' q_median_household_income_2018 [aweight=pop_estimate_2018], a(effective_date) cluster(state_fips)
		quietly est sto reg1
		
		areg `var' q_gop_win_pct [aweight=pop_estimate_2018], a(effective_date) cluster(state_fips)
		quietly est sto reg2
		
		areg `var' q_ruralurban_continuum_code_2013 [aweight=pop_estimate_2018], a(effective_date) cluster(state_fips)
		quietly est sto reg3
		
		areg `var' q_median_household_income_2018 q_gop_win_pct q_ruralurban_continuum_code_2013 [aweight=pop_estimate_2018], a(effective_date) cluster(state_fips)
		quietly est sto reg4
		
	 
		esttab reg1 reg2 reg3 reg4 using "${tables}/`var'_2bins_v5.tex", stats(N r2 mean_d, label("Observations" "R$^2$" "Mean dep. var.")  ///
		fmt( %12.0fc %12.3fc %12.4gc  %12.3fc)) substitute(\_ _) booktabs b(%12.2fc) se(%12.2fc) nomti nonote ///
		order(q_median_household_income_2018 q_gop_win_pct q_ruralurban_continuum_code_2013) ///
			varlabel(_cons "Constant" q_median_household_income_2018 "Above-median income"  q_gop_win_pct "Republican-supporting" q_ruralurban_continuum_code_2013 "Rural") ///
		style(tex) star( * 0.1 ** 0.05 *** 0.01) replace compress noeqlines obslast
		
		restore

	}
	
}

sum d_visit_event d_distance_event d_pe_r_food_event d_log_pe_food_event [aweight=pop_estimate_2018]

log close

